package com.yanqu.road.dao.impl.activity.douluodalu;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.douluodalu.data.DouLuoDaLuTeamData;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class DouLuoDaLuTeamDataDaoImpl extends TempDataObjectDao<DouLuoDaLuTeamData> {


    public int getMaxTeamId(int activityId, int groupId) {
        String sql = "select ifnull(max(teamId),0) as maxId from t_c_douluodalu_team where activityId = ? and groupId=?";
        ResultSet rs = null;
        CallableStatement call = null;
        int maxId = 0;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setInt(i++,activityId);
            call.setInt(i++,groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                maxId = rs.getInt("maxId");
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return maxId;
    }


    public static String getDouLuoDaLuTeamDataSql = "select * from `t_c_douluodalu_team` where `activityId`=? and groupId=? and `isDelete` = 0";
    public Map<Integer, DouLuoDaLuTeamData> getDouLuoDaLuTeamData(int activityId, int groupId) {
        ResultSet rs = null;
        CallableStatement call = null;
        Map<Integer, DouLuoDaLuTeamData> result = new ConcurrentHashMap<>();
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getDouLuoDaLuTeamDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            call.setInt(i++, groupId);
            rs = call.executeQuery();
            while (rs.next()) {
                DouLuoDaLuTeamData data = parse(rs);
                result.put(data.getTeamId(), data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);} finally {
            getDb().closeConnection(call, rs);
        }
        return result;
    }

    public static String insertDouLuoDaLuTeamDataSql = "insert into `t_c_douluodalu_team`(activityId,teamId,captainId,captainServerId,name,createTime,memberList,applyList,isDelete,groupId) values (?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(DouLuoDaLuTeamData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTeamId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCaptainId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCaptainServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getName()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMemberList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getApplyList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDelete()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(insertDouLuoDaLuTeamDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateDouLuoDaLuTeamDataSql = "update `t_c_douluodalu_team` set captainId=?,captainServerId=?,name=?,createTime=?,memberList=?,applyList=?,isDelete=? where activityId=? and teamId=? and groupId=?";

    @Override
    protected boolean updateData(DouLuoDaLuTeamData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<Integer, DbParameter>();
        para.put(i++, new DbParameter(Types.BIGINT, data.getCaptainId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCaptainServerId()));
        para.put(i++, new DbParameter(Types.VARCHAR, data.getName()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getCreateTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMemberList())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getApplyList())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isDelete()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTeamId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getGroupId()));
        if (getDb().executeNoneQuery(updateDouLuoDaLuTeamDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected DouLuoDaLuTeamData parseData(ResultSet rs) throws SQLException {
        DouLuoDaLuTeamData data = new DouLuoDaLuTeamData();
        data.setActivityId(rs.getInt("activityId"));
        data.setTeamId(rs.getInt("teamId"));
        data.setCaptainId(rs.getLong("captainId"));
        data.setCaptainServerId(rs.getLong("captainServerId"));
        data.setName(rs.getString("name"));
        data.setCreateTime(rs.getLong("createTime"));
        data.setMemberList(JSON.parseObject(rs.getString("memberList"), new TypeReference<ConcurrentHashMap<Long, Long>>() {}));
        data.setApplyList(JSON.parseObject(rs.getString("applyList"), new TypeReference<ConcurrentHashMap<Long, Long>>() {}));
        data.setDelete(rs.getBoolean("isDelete"));
        data.setGroupId(rs.getInt("groupId"));

        return data;
    }
}
